home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / Common / d3dMesh.cls < prev    next >
Text File  |  2001-10-08  |  26KB  |  746 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "CD3DMesh"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14.  
  15. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  16. '
  17. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  18. '
  19. '  File:       D3DMesh.cls
  20. '  Content:    D3D VB Framework Mesh
  21. '
  22. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  23.  
  24.  
  25. Option Explicit
  26.  
  27. Private Type BoneComboIdList
  28.     List(4) As Long
  29. End Type
  30.  
  31.  
  32. ' Base Objects
  33. Public mesh As D3DXMesh                         'if not skinned, regular mesh object
  34. Public skinmesh As D3DXSkinMesh                 'if skinned - skinned mesh object
  35. Public bUseMaterials As Boolean                 'Use materials in object
  36. Public bUseMaterialOverride As Boolean          'Use only override material
  37. Public ObjectName As String                     'Name of object
  38. Public bSkinned As Boolean                      'Inidicates if the object is a skin
  39.  
  40. ' Culling objects
  41. Dim m_SphereCenter As D3DVECTOR                 'center of bounding sphere
  42. Public SphereRadius As Single                   'radius of bounding sphere
  43. Public bHasSphere As Boolean                    'Inidcatges if bounding sphere is calculated
  44.  
  45. ' Material and Adjacency information
  46. Dim m_MaterialOverride As D3DMATERIAL8          'Override material to use if bUseMaterialOverride set
  47. Dim m_NumMaterials As Long                      'Number of materials in object
  48. Dim m_Materials() As D3DMATERIAL8               'Material List
  49. Dim m_Textures() As Direct3DTexture8            'Texture List
  50. Dim m_TextureNames() As String                  'Texture List Names
  51. Dim m_AdjBuffer As D3DXBuffer                   'Adjacency buffer for the mesh
  52.  
  53.  
  54. ' Bone and skinning informatin
  55. Dim m_BoneNames As D3DXBuffer                   'Names of Frame objects that are bones
  56. Dim m_BoneMatrices As D3DXBuffer                'Matrix object per bone
  57. Dim m_BoneComboTable As D3DXBuffer              'Groupings of bone material and triangles
  58. Dim m_BoneFrames() As CD3DFrame                 'Frame objects that are bones
  59. Dim m_maxFaceInfl As Long                       'Number of matrices that will influence a vertex
  60. Dim m_worldMatHandles(4) As Long                'handle to multiple world matrices
  61. Dim m_BoneOffsetMat() As D3DMATRIX              'Bone offset matrices from D3DXBuffers
  62. Dim m_NumBoneCombos As Long                     'Size of bonecombo table
  63. Dim m_BoneCombos() As D3DXBONECOMBINATION       'Combo table as returned from D3DX
  64. Dim m_BoneComboIds() As BoneComboIdList         'BoneId portion of combotable
  65. Dim m_BoneCount As Long                         'Number of bones
  66. Dim m_bonesAttached As Boolean                  'Indicates if bones have been attached to the mesh
  67.  
  68. '-----------------------------------------------------------------------------
  69. ' Init
  70. '-----------------------------------------------------------------------------
  71. Sub Init()
  72.     bUseMaterials = True
  73. End Sub
  74.  
  75. '-----------------------------------------------------------------------------
  76. ' SphereCenter()
  77. ' returns Sphere Center since D3DVECTOR can not be public variable return value
  78. '-----------------------------------------------------------------------------
  79. Property Get SphereCenter() As D3DVECTOR
  80.     SphereCenter = m_SphereCenter
  81. End Property
  82.  
  83. '-----------------------------------------------------------------------------
  84. ' Name: InitFromFile
  85. ' Desc: Tries first to load mesh in current directory or using explicit path
  86. '       If that fails loads mesh from SDK media path
  87. '-----------------------------------------------------------------------------
  88.  
  89. Public Function InitFromFile(dev As Direct3DDevice8, Name As String) As Boolean
  90.     Dim mtrlBuffer As D3DXBuffer
  91.     Dim strPath As String
  92.     Destroy
  93.     
  94. '    On Local Error Resume Next
  95.     Set m_AdjBuffer = Nothing
  96.     bSkinned = False
  97.     
  98.     Set mesh = g_d3dx.LoadMeshFromX(Name, D3DXMESH_MANAGED, dev, m_AdjBuffer, mtrlBuffer, m_NumMaterials)
  99.     If Err.Number <> 0 Then
  100.         Err.Clear
  101.         On Local Error GoTo errOut
  102.         strPath = g_mediaPath + Name
  103.         Set mesh = g_d3dx.LoadMeshFromX(strPath, D3DXMESH_MANAGED, dev, m_AdjBuffer, mtrlBuffer, m_NumMaterials)
  104.     
  105.     End If
  106.     
  107.     Call InitMaterials(g_dev, mtrlBuffer)
  108.     
  109.     InitFromFile = True
  110.     Exit Function
  111.     
  112. errOut:
  113.     InitFromFile = False
  114. End Function
  115.  
  116.  
  117.  
  118. '-----------------------------------------------------------------------------
  119. ' Name: InitFromXOF
  120. ' Desc: Load mesh from data provided by XOF api
  121. '       Called from D3DUtil_LoadFromFile
  122. '-----------------------------------------------------------------------------
  123.  
  124. Public Function InitFromXOF(dev As Direct3DDevice8, meshdata As DirectXFileData) As Boolean
  125.     Dim mtrlBuffer As D3DXBuffer
  126.     Dim bonename As String
  127.     Dim i As Long
  128.     Dim q As Long
  129.         
  130.         
  131.     Destroy
  132.     
  133.     
  134.     Set m_AdjBuffer = Nothing
  135.     Set m_BoneMatrices = Nothing
  136.     Set m_BoneNames = Nothing
  137.     Set mesh = Nothing
  138.     Set skinmesh = Nothing
  139.     Set m_BoneMatrices = Nothing
  140.     Set m_BoneComboTable = Nothing
  141.     
  142.     ObjectName = meshdata.GetName()
  143.     
  144. '    On Local Error GoTo errOut
  145.     bSkinned = False
  146.     
  147.     'Users can set this variable to TRUE try the skinned load path
  148.     If g_bLoadSkins = True Then
  149.         Set skinmesh = g_d3dx.LoadSkinMeshFromXof(meshdata, D3DXMESH_MANAGED, dev, m_AdjBuffer, mtrlBuffer, m_NumMaterials, m_BoneNames, m_BoneMatrices)
  150.         
  151.         
  152.         Dim pAdj As Long, AdjOut As D3DXBuffer
  153.         pAdj = m_AdjBuffer.GetBufferPointer
  154.         
  155.         m_BoneCount = skinmesh.GetNumBones()
  156.         
  157.         If m_BoneCount = 0 Then
  158.             
  159.             ''''''''''''''''''''''''''''''''''''''''''''''''''''
  160.             ' a skinned mesh with no bones is just a regular mesh
  161.             ''''''''''''''''''''''''''''''''''''''''''''''''''''
  162.             
  163.             bSkinned = False
  164.             Set mesh = skinmesh.GetOriginalMesh()
  165.             'Set skinmesh = Nothing
  166.             
  167.         Else
  168.         
  169.             '''''''''''''''''''''''''''''''''''''''''''''''
  170.             ' code specific to x files with skinning data in them
  171.             '''''''''''''''''''''''''''''''''''''''''''''''
  172.             
  173.             bSkinned = True
  174.             
  175.             Set mesh = skinmesh.ConvertToBlendedMesh(D3DXMESH_SYSTEMMEM, ByVal pAdj, ByVal 0, m_NumBoneCombos, m_BoneComboTable, ByVal 0&, Nothing)
  176.             Set m_AdjBuffer = Nothing
  177.             Set m_AdjBuffer = AdjOut
  178.             Set AdjOut = Nothing
  179.             
  180.             'retrieve number of influence (matrices) that a vertices could have
  181.             'we support up to 4 corresponding to the 4 world matrices that can be set
  182.             m_maxFaceInfl = skinmesh.GetMaxFaceInfluences()
  183.             
  184.             m_worldMatHandles(0) = D3DTS_WORLD
  185.             m_worldMatHandles(1) = D3DTS_WORLD1
  186.             m_worldMatHandles(2) = D3DTS_WORLD2
  187.             m_worldMatHandles(3) = D3DTS_WORLD3
  188.             
  189.             ReDim m_BoneCombos(m_NumBoneCombos)
  190.             ReDim m_BoneComboIds(m_NumBoneCombos)
  191.             
  192.             ' fill in our private table for bone combo data
  193.             ' this inidicates which bones (matrices) need to be blended
  194.             ' for a given subset in the mesh
  195.             For q = 0 To m_NumBoneCombos - 1
  196.                 g_d3dx.BufferGetBoneCombo m_BoneComboTable, q, m_BoneCombos(q)
  197.                 g_d3dx.BufferGetBoneComboBoneIds m_BoneComboTable, q, m_maxFaceInfl, m_BoneComboIds(q).List(0)
  198.             Next
  199.             Set m_BoneComboTable = Nothing
  200.                     
  201.             ' fill in our private table for bone offset matrices
  202.             ' these are the matrices that give the intitial displacement of mesh subsets
  203.             ' release the d3dx buffer to save memory
  204.             ReDim m_BoneOffsetMat(m_BoneCount)
  205.             g_d3dx.BufferGetData m_BoneMatrices, 0, Len(m_BoneOffsetMat(0)), m_BoneCount, m_BoneOffsetMat(0)
  206.             Set m_BoneMatrices = Nothing
  207.             
  208.             
  209.         End If
  210.     Else
  211.     
  212.          Set mesh = g_d3dx.LoadMeshFromXof(meshdata, D3DXMESH_MANAGED, dev, m_AdjBuffer, mtrlBuffer, m_NumMaterials)
  213.          
  214.     End If
  215.     
  216.     
  217.     Call InitMaterials(g_dev, mtrlBuffer)
  218.     
  219.     InitFromXOF = True
  220.     Exit Function
  221.     
  222. errOut:
  223.     InitFromXOF = False
  224. End Function
  225.  
  226.  
  227. '-----------------------------------------------------------------------------
  228. ' Name: AttatchBonesToMesh
  229. ' Desc: Called to attach bones to a skin.
  230. '       The BoneNames table is used to search out bone frames
  231. '       in the children of the given parent frame
  232. '
  233. '       This must be done for any skinning animation to work
  234. '-----------------------------------------------------------------------------
  235.  
  236. Friend Sub AttatchBonesToMesh(parent As CD3DFrame)
  237.       ' get links to all the frames (bones)
  238.         Dim i As Long
  239.         Dim bonename As String
  240.         ReDim m_BoneFrames(m_BoneCount)
  241.         For i = 0 To m_BoneCount - 1
  242.             bonename = g_d3dx.BufferGetBoneName(m_BoneNames, i)
  243.             Set m_BoneFrames(i) = parent.FindChildObject(bonename, 0)
  244.             If m_BoneFrames(i) Is Nothing Then
  245.                 Debug.Print "unable to find " + bonename
  246.                 Stop
  247.             End If
  248.         Next
  249.         m_bonesAttached = True
  250.         Set m_BoneNames = Nothing
  251. End Sub
  252.  
  253.  
  254. '-----------------------------------------------------------------------------
  255. ' Name: Optimize
  256. ' Desc: Re-organize the mesh for better performance
  257. '
  258. '-----------------------------------------------------------------------------
  259.  
  260. Sub Optimize()
  261.     Dim s As Long
  262.     Dim adjBuf1() As Long
  263.     Dim adjBuf2() As Long
  264.     Dim facemap() As Long
  265.     Dim newmesh As D3DXMesh
  266.     Dim vertexMap As D3DXBuffer
  267.     
  268.     s = m_AdjBuffer.GetBufferSize
  269.     ReDim adjBuf1(s / 4)
  270.     ReDim adjBuf2(s / 4)
  271.     
  272.     s = mesh.GetNumFaces
  273.     ReDim facemap(s)
  274.     
  275.     g_d3dx.BufferGetData m_AdjBuffer, 0, 4, s * 3, adjBuf1(0)
  276.     
  277.     Set newmesh = mesh.Optimize(D3DXMESHOPT_ATTRSORT Or D3DXMESHOPT_VERTEXCACHE, adjBuf1(0), adjBuf2(0), facemap(0), vertexMap)
  278.     
  279.     If Not newmesh Is Nothing Then
  280.         Set mesh = Nothing
  281.         Set mesh = newmesh
  282.     End If
  283.  
  284. End Sub
  285.  
  286.  
  287.  
  288.  
  289. '-----------------------------------------------------------------------------
  290. ' Name: InitMaterials
  291. ' Desc: Helper function for creating mesh materials
  292. '       called after initialization
  293. '-----------------------------------------------------------------------------
  294. Private Sub InitMaterials(d3ddevice As Direct3DDevice8, mtrlBuffer As D3DXBuffer)
  295.     Dim i As Long
  296.     
  297.     If m_NumMaterials <= 0 Then Exit Sub
  298.     
  299.     ReDim m_Materials(m_NumMaterials)
  300.     ReDim m_Textures(m_NumMaterials)
  301.     ReDim m_TextureNames(m_NumMaterials)
  302.  
  303.     For i = 0 To m_NumMaterials - 1
  304.         'copy material out of material buffer into our own structure
  305.         g_d3dx.BufferGetMaterial mtrlBuffer, i, m_Materials(i)
  306.  
  307.         If g_bLoadNoAlpha Then m_Materials(i).diffuse.a = 1
  308.         
  309.         m_Materials(i).Ambient = m_Materials(i).diffuse
  310.         
  311.         m_TextureNames(i) = g_d3dx.BufferGetTextureName(mtrlBuffer, i)
  312.         
  313.         If g_bUseTextureLoadCallback Then
  314.             Set m_Textures(i) = g_TextureLoadCallback.TextureLoadCallback(m_TextureNames(i))
  315.         Else
  316.             Set m_Textures(i) = D3DUtil_CreateTextureInPool(g_dev, m_TextureNames(i), D3DFMT_UNKNOWN)
  317.         End If
  318.     Next
  319.     
  320.     
  321. End Sub
  322.  
  323.  
  324.  
  325. '-----------------------------------------------------------------------------
  326. ' Name: SetFVF
  327. ' Desc: Change the FVF of the current mesh
  328. '----------------------------------------------------------------------------
  329. Public Sub SetFVF(dev As Direct3DDevice8, fvf As Long)
  330.     
  331.     Dim tempMesh As D3DXMesh
  332.     Dim verts() As D3DVERTEX
  333.  
  334.     If mesh Is Nothing Then Exit Sub
  335.     
  336.     Set tempMesh = mesh.CloneMeshFVF(D3DXMESH_MANAGED, fvf, dev)
  337.     
  338.         
  339.     Set mesh = tempMesh
  340.     
  341.     
  342. End Sub
  343.  
  344. '-----------------------------------------------------------------------------
  345. ' Name: GenerateNormals
  346. ' Desc: if the current mesh Flexible Vertex Format (FVF) has normals in it
  347. '       that are not initialized. This function will fill them.
  348. '       if no normals are present in the FVF this function will fire an
  349. '       exception
  350. '----------------------------------------------------------------------------
  351. Public Sub ComputeNormals()
  352.     Dim bm As D3DXBaseMesh
  353.     Set bm = mesh
  354.     g_d3dx.ComputeNormals bm
  355. End Sub
  356.  
  357.  
  358. '-----------------------------------------------------------------------------
  359. ' Name: FlipNormals
  360. ' Desc: Convenience function that flips normals for a D3DVERTEX mesh (default)
  361. '----------------------------------------------------------------------------
  362. Public Sub FlipNormals()
  363.     Dim count As Long
  364.     Dim size As Long
  365.     Dim i As Long
  366.     
  367.     Dim verts() As D3DVERTEX
  368.     
  369.     Dim vb As Direct3DVertexBuffer8
  370.     Set vb = mesh.GetVertexBuffer()
  371.     
  372.     
  373.     
  374.     
  375.     size = g_d3dx.GetFVFVertexSize(mesh.GetFVF())
  376.     count = mesh.GetNumVertices()
  377.     
  378.     
  379.     
  380.     If mesh.GetFVF() = D3DFVF_VERTEX Then
  381.         ReDim verts(count)
  382.         
  383.         D3DVertexBuffer8GetData vb, 0, size * count, 0, verts(0)
  384.         
  385.         For i = 0 To count - 1
  386.             verts(i).nx = -verts(i).nx
  387.             verts(i).ny = -verts(i).ny
  388.             verts(i).nz = -verts(i).nz
  389.         Next
  390.         
  391.         D3DVertexBuffer8SetData vb, 0, size * count, 0, verts(0)
  392.     Else
  393.         Stop
  394.     End If
  395.     
  396.  
  397. End Sub
  398.  
  399.  
  400. '-----------------------------------------------------------------------------
  401. ' Name: Translate
  402. ' Desc: all vertices are moved by x,y,z
  403. '       note that object will still rotate about 0,0,0
  404. '
  405. '----------------------------------------------------------------------------
  406. Public Sub Translate(x As Single, y As Single, z As Single)
  407.     Dim count As Long
  408.     Dim size As Long
  409.     Dim i As Long
  410.     
  411.     Dim verts() As D3DVERTEX
  412.     
  413.     Dim vb As Direct3DVertexBuffer8
  414.     Set vb = mesh.GetVertexBuffer()
  415.     
  416.     
  417.     
  418.     size = g_d3dx.GetFVFVertexSize(mesh.GetFVF())
  419.     count = mesh.GetNumVertices()
  420.  
  421.     
  422.     If mesh.GetFVF() = D3DFVF_VERTEX Then
  423.         ReDim verts(count)
  424.         
  425.         D3DVertexBuffer8GetData vb, 0, size * count, 0, verts(0)
  426.         
  427.         For i = 0 To count - 1
  428.             verts(i).x = verts(i).x + x
  429.             verts(i).y = verts(i).y + y
  430.             verts(i).z = verts(i).z + z
  431.         Next
  432.         
  433.         D3DVertexBuffer8SetData vb, 0, size * count, 0, verts(0)
  434.         
  435.     End If
  436.     
  437.  
  438. End Sub
  439.  
  440.  
  441. '-----------------------------------------------------------------------------
  442. ' Name: GetLocalBox
  443. ' Desc: Returns the extent of the mesh in the local coordinate system
  444. '----------------------------------------------------------------------------
  445. Public Sub GetLocalBox(MinExt As D3DVECTOR, MaxExt As D3DVECTOR)
  446.     g_d3dx.ComputeBoundingBoxFromMesh mesh, MinExt, MaxExt
  447. End Sub
  448.  
  449. '-----------------------------------------------------------------------------
  450. ' Name: Destroy
  451. ' Desc: release any reference to frame and texture objects
  452. '-----------------------------------------------------------------------------
  453. Sub Destroy()
  454.     
  455.     'Releases all objects (does leave 1 element in the array)
  456.     ReDim m_Textures(0)
  457.     ReDim m_Materials(0)
  458.     ReDim m_TextureNames(0)
  459.     ReDim m_BoneFrames(0)
  460.     ReDim m_BoneOffsetMat(0)
  461.     ReDim m_BoneCombos(0)
  462.     m_NumMaterials = 0
  463.     bUseMaterials = True
  464.     Set mesh = Nothing
  465.     Set skinmesh = Nothing
  466. End Sub
  467.  
  468.  
  469. '-----------------------------------------------------------------------------
  470. ' Name: ComputeBoundingVolumes
  471. ' Desc: Makes BoundingSphere valid
  472. '-----------------------------------------------------------------------------
  473. Public Sub ComputeBoundingVolumes()
  474.     g_d3dx.ComputeBoundingSphereFromMesh mesh, m_SphereCenter, SphereRadius
  475.     bHasSphere = True
  476. End Sub
  477.  
  478.  
  479. '-----------------------------------------------------------------------------
  480. ' Name: RenderEx
  481. ' Desc: Render Mesh
  482. ' Params:
  483. '   dev                         the device to draw to
  484. '   bDrawOpaqueSubsets          draws all triangles that do not have alpha
  485. '   bDrawOpaqueSubsets          draws all triangles that have alpha
  486. '                               (note Blending renderstates are modified)
  487. '
  488. ' Note: do not use for skinned meshes
  489. '-----------------------------------------------------------------------------
  490. Sub RenderEx(dev As Direct3DDevice8, bDrawOpaqueSubsets As Boolean, bDrawAlphaSubsets As Boolean)
  491.     If mesh Is Nothing Then Exit Sub
  492.     Dim i As Long
  493.     
  494.     'If bSkinned = True Then Exit Sub
  495.  
  496.     ' Frist, draw the subsets without alpha
  497.     If (bDrawOpaqueSubsets) Then
  498.         For i = 0 To m_NumMaterials - 1
  499.         
  500.                 
  501.         
  502.             If (bUseMaterials) Then
  503.                 If m_Materials(i).diffuse.a = 1# Then
  504.                     g_dev.SetMaterial m_Materials(i)
  505.                     
  506.                     If g_bDontDrawTextures Then
  507.                         g_dev.SetTexture 0, Nothing
  508.                     Else
  509.                         g_dev.SetTexture 0, m_Textures(i)
  510.                     End If
  511.                     
  512.                     mesh.DrawSubset i
  513.                 End If
  514.             ElseIf (bUseMaterialOverride) Then
  515.                 If m_MaterialOverride.diffuse.a = 1# Then
  516.                     If g_bDontDrawTextures Then
  517.                         g_dev.SetTexture 0, Nothing
  518.                     Else
  519.                         g_dev.SetTexture 0, m_Textures(i)
  520.                     End If
  521.                     g_dev.SetMaterial m_MaterialOverride
  522.                     mesh.DrawSubset i
  523.                 End If
  524.             Else
  525.                 mesh.DrawSubset i
  526.             End If
  527.                     
  528.         Next
  529.     End If
  530.  
  531.     
  532.     ' Then, draw the subsets with alpha
  533.     If (bDrawAlphaSubsets And (bUseMaterialOverride Or bUseMaterials)) Then
  534.         For i = 0 To m_NumMaterials - 1
  535.         
  536.             If (bUseMaterials) Then
  537.                     If (m_Materials(i).diffuse.a < 1#) Then
  538.                           g_dev.SetMaterial m_Materials(i)
  539.                           g_dev.SetTexture 0, m_Textures(i)
  540.                           mesh.DrawSubset i
  541.                     End If
  542.             ElseIf (bUseMaterialOverride) Then
  543.                     If (m_MaterialOverride.diffuse.a < 1#) Then
  544.                           g_dev.SetMaterial m_MaterialOverride
  545.                           g_dev.SetTexture 0, m_Textures(i)
  546.                           mesh.DrawSubset i
  547.                     End If
  548.             End If
  549.         Next
  550.     End If
  551.     
  552. End Sub
  553.  
  554. '-----------------------------------------------------------------------------
  555. ' Name: Render
  556. ' Desc: Render the mesh to the given device
  557. '
  558. ' Note: Do not use for skinned meshes
  559. '
  560. '-----------------------------------------------------------------------------
  561. Sub Render(dev As Direct3DDevice8)
  562.     Dim i As Long
  563.     
  564.     If mesh Is Nothing Then Exit Sub
  565.     
  566.     If bSkinned = True Then Exit Sub
  567.     
  568.     If (bUseMaterials) Then
  569.         For i = 0 To m_NumMaterials - 1
  570.             g_dev.SetMaterial m_Materials(i)
  571.             g_dev.SetTexture 0, m_Textures(i)
  572.             mesh.DrawSubset i
  573.         Next
  574.     Else
  575.         For i = 0 To m_NumMaterials - 1
  576.             mesh.DrawSubset i
  577.         Next
  578.  
  579.     End If
  580.         
  581. End Sub
  582.  
  583. '-----------------------------------------------------------------------------
  584. ' Name: RenderSkin
  585. ' Desc: Render the Mesh as skin
  586. ' Note: The mesh must have been loaded as a skin and bones must have been attached
  587. '-----------------------------------------------------------------------------
  588. Sub RenderSkin()
  589.  
  590.     If Not bSkinned Then Exit Sub
  591.     
  592.     Dim ipAttr As Long  'bonecombo attribute
  593.     Dim matId As Long   'matrix id
  594.     
  595.     Dim i As Long
  596.     Dim mat2 As D3DMATRIX
  597.     Dim mat1 As D3DMATRIX
  598.     Dim mat0 As D3DMATRIX
  599.  
  600.     
  601.     g_dev.SetRenderState D3DRS_VERTEXBLEND, m_maxFaceInfl - 1
  602.         
  603.     For ipAttr = 0 To m_NumBoneCombos - 1
  604.         For i = 0 To m_maxFaceInfl - 1
  605.                 matId = m_BoneComboIds(ipAttr).List(i)
  606.                 
  607.                 'If we get a MatId of -1 then all the vertex weights are 0
  608.                 'and we dont need to set the transform for this bone
  609.                 If matId <> -1 Then
  610.                     mat0 = m_BoneFrames(matId).GetUpdatedMatrix()
  611.                     mat1 = m_BoneOffsetMat(matId)
  612.                     D3DXMatrixMultiply mat2, mat1, mat0
  613.                     g_dev.SetTransform m_worldMatHandles(i), mat2
  614.                 End If
  615.         Next
  616.         g_dev.SetTexture 0, m_Textures(m_BoneCombos(ipAttr).AttribId)
  617.         g_dev.SetMaterial m_Materials(m_BoneCombos(ipAttr).AttribId)
  618.         
  619.         mesh.DrawSubset ipAttr
  620.         
  621.     Next
  622.     
  623.     g_dev.SetRenderState D3DRS_VERTEXBLEND, 0
  624.     
  625.     
  626. End Sub
  627.  
  628. '-----------------------------------------------------------------------------
  629. ' Name: GetMaterialCount
  630. '
  631. '---------------------------------------------------------------------------
  632. Public Function GetMaterialCount() As Long
  633.     GetMaterialCount = m_NumMaterials
  634. End Function
  635.  
  636.  
  637. '-----------------------------------------------------------------------------
  638. ' Name: SetMaterialOverride
  639. ' Desc: Sets the materail to be used in place of the ones loaded from file
  640. ' Note: to disable set bUseMaterialOverride to false
  641. '-----------------------------------------------------------------------------
  642. Public Sub SetMaterialOverride(m As D3DMATERIAL8)
  643.     m_MaterialOverride = m
  644.     bUseMaterialOverride = True
  645. End Sub
  646.  
  647. '-----------------------------------------------------------------------------
  648. ' Name: GetMaterialOverride
  649. ' Desc:
  650. '-----------------------------------------------------------------------------
  651. Public Sub GetMaterialOverride(m As D3DMATERIAL8)
  652.       m = m_MaterialOverride
  653. End Sub
  654.  
  655.  
  656. '-----------------------------------------------------------------------------
  657. ' Name: ClassName
  658. ' Desc:
  659. '-----------------------------------------------------------------------------
  660. Public Function ClassName() As String
  661.     ClassName = "CD3DMesh"
  662. End Function
  663.  
  664.  
  665. '-----------------------------------------------------------------------------
  666. ' Name: InvalidateDeviceObjects
  667. ' Desc: Release reference to device dependent objects
  668. '-----------------------------------------------------------------------------
  669. Public Sub InvalidateDeviceObjects()
  670.    'all framework objects are managed so nothing to do here
  671. End Sub
  672.  
  673.  
  674.  
  675. '-----------------------------------------------------------------------------
  676. ' Name: RestoreDeviceObjects
  677. ' Desc: If we had any video memory objects they would need
  678. '       to be reloaded here
  679. '-----------------------------------------------------------------------------
  680. Public Sub RestoreDeviceObjects(dev As Direct3DDevice8)
  681.     
  682. End Sub
  683.  
  684.  
  685. '-----------------------------------------------------------------------------
  686. ' Name: InitFromD3DXMesh
  687. ' Desc: Allow mesh objects to be created from external D3DXMesh objects
  688. '
  689. '-----------------------------------------------------------------------------
  690. Sub InitFromD3DXMesh(d3dxmeshIn As D3DXMesh)
  691.     bUseMaterials = False
  692.     ReDim m_Materials(1)
  693.     ReDim m_Textures(1)
  694.     m_NumMaterials = 1
  695.     Set mesh = d3dxmeshIn
  696. End Sub
  697.  
  698.  
  699. '-----------------------------------------------------------------------------
  700. ' Name: SetMaterialCount
  701. ' Desc: If a mesh was initialized with InitFromD3DXMesh
  702. '       This function can allocate space for Materials and Textures
  703. '-----------------------------------------------------------------------------
  704. Sub SetMaterialCount(n As Long)
  705.     m_NumMaterials = n
  706.     ReDim Preserve m_Materials(n)
  707.     ReDim Preserve m_Textures(n)
  708. End Sub
  709.  
  710. '-----------------------------------------------------------------------------
  711. ' Name: SetMaterialTexture
  712. ' Desc: Sets the texture for a given material subset
  713. ' Note: use nothing to remove a texture
  714. '-----------------------------------------------------------------------------
  715. Sub SetMaterialTexture(n As Long, tex As Direct3DTexture8)
  716.     Set m_Textures(n) = tex
  717. End Sub
  718.  
  719. '-----------------------------------------------------------------------------
  720. ' Name: GetMaterialTexture
  721. ' Desc: returns a given texture for a material subset
  722. '-----------------------------------------------------------------------------
  723. Function GetMaterialTexture(n As Long) As Direct3DTexture8
  724.     Set GetMaterialTexture = m_Textures(n)
  725. End Function
  726.  
  727.  
  728. '-----------------------------------------------------------------------------
  729. ' Name: SetMaterial
  730. ' Desc: Sets the material properties for a given material subset
  731. '-----------------------------------------------------------------------------
  732. Sub SetMaterial(n As Long, material As D3DMATERIAL8)
  733.     m_Materials(n) = material
  734. End Sub
  735.  
  736.  
  737. '-----------------------------------------------------------------------------
  738. ' Name: GetMaterial
  739. ' Desc: returns material properties for a material subset
  740. '-----------------------------------------------------------------------------
  741. Function GetMaterial(n As Long) As D3DMATERIAL8
  742.     GetMaterial = m_Materials(n)
  743. End Function
  744.  
  745.  
  746.